﻿function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function guid() {
    return (S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4());
}

function SetupContentTypeSelect(element) {
    $(element).multiselect({
        noneSelectedText: 'Select content types',
        selectedList: 99,
        header: false,
        click: function (event, ui) {
            if (ui.value == '*') {
                if (ui.checked) {
                    $(event.srcElement).parents('li').siblings('li').addClass('ui-state-disabled');
                    $(event.srcElement).parents('li').siblings('li').find('input').attr('disabled', 'disabled').removeAttr('checked');
                } else {
                    $(event.srcElement).parents('li').siblings('li').removeClass('ui-state-disabled');
                    $(event.srcElement).parents('li').siblings('li').find('input').removeAttr('disabled');
                }
            }

        },
        beforeopen: function () {
            var menu = $(this).siblings('.ui-multiselect-menu');            
            if ($(menu).find('input[value="*"]').is(":checked")) {
                $(menu).find('input[value!="*"]').parents('li').addClass('ui-state-disabled');
                $(menu).find('input[value!="*"]').attr('disabled', 'disabled').removeAttr('checked');
            }
        },
        close: function () {
            var values = $.map($(this).siblings('.ui-multiselect-menu').find('input:checked'), function (checkbox) {
                return checkbox.value;
            }).join(",");
            $(this).siblings('input[type="hidden"][id$="ContentTypeLimitations"]').val(values);
        }
    });
}

$(document).ready(function () {
    $('#butAddNewRelationship').click(function () {
        var template = $('#related-content-new-template').children(':first').clone();
        var tempID = guid();
        $(template).find('[name*="template[0]"]').each(function () {
            $(this).attr('name', $(this).attr('name').replace("template[0]", "Relationships[" + tempID + "]"));
            $(this).attr('id', $(this).attr('id').replace("template_0", "Relationships_" + tempID + ""));
        });
        $(template).find('input[name$="template.Index"]').each(function () {
            $(this).attr('name', $(this).attr('name').replace("template.Index", "Relationships.Index"));
            $(this).val(tempID);
        });
        $(template).appendTo('#related-content-relationships');
        SetupContentTypeSelect($(template).find(".ContentTypeLimitationSelect"));
        return false;
    });

    SetupContentTypeSelect($('#related-content-relationships .ContentTypeLimitationSelect'));

    $('.remove-content-relationship').click(function () {
        $(this).parents('fieldset:first').slideUp('fast', function () { $(this).remove(); });
        return false;
    });

    $('.advanced-content-relationship-toggle').click(function () {
        $(this).siblings('.advanced-content-relationship-settings').slideToggle('fast', function() {
            if ($(this).is(':visible')) {
                $(this).siblings('.advanced-content-relationship-toggle').html('Hide Advanced Settings');
            } else {
                $(this).siblings('.advanced-content-relationship-toggle').html('Show Advanced Settings');
            }
        });
        return false;
    });
});